#include "DrawList.h"

CDrawList::CDrawList(void)
{
	m_iBricksCount = 0;
	m_iBallsCount = 0;
}

CDrawList::~CDrawList(void)
{
}

int CDrawList::AddObject(CDrawable* pDrawable)
{
	m_vObjList.push_back(pDrawable);	//push new object to the end of vector
	//if it is brick, increase brick counter
	if(pDrawable->GetType() == TYPE_BRICK) 
	{
		m_iBricksCount++;
	}
	if(pDrawable->GetType() == TYPE_BALL) 
	{
		m_iBallsCount++;
	}
	return 0;
}

int CDrawList::MarkObject(CDrawable *pDrawable)
{
	m_vObjMarked.push_back(pDrawable);
	return 0;
}

/*************************************************************************/
/* Remove marked entries                                                 */
/*************************************************************************/
int CDrawList::Clean()
{
	for(unsigned int i=0; i<m_vObjMarked.size(); i++)
	{
		//find object to delete from ObjList
		vector<CDrawable*>::iterator iterMarked;
		iterMarked = find(m_vObjList.begin(), m_vObjList.end(), m_vObjMarked[i]);

		//check if this object exists on the list
		if(iterMarked != m_vObjList.end())
		{
			//if so, do the cleaning
			CDrawable* pDrawable = (*iterMarked);
			//if it is brick, decrease brick counter
			if(pDrawable->GetType() == TYPE_BRICK) 
			{
				m_iBricksCount--;
			}
			if(pDrawable->GetType() == TYPE_BALL) 
			{
				m_iBallsCount--;
			}
			m_vObjList.erase(iterMarked);
			delete pDrawable;
		}
	}

	//empty marked list
	m_vObjMarked.empty();

	return 0;
}

/**********************************************************************/
/* Empty whole list - remove all entries                              */
/**********************************************************************/
int CDrawList::Empty()
{
	//while something in the vector...
	while(m_vObjList.begin() != m_vObjList.end())
	{
		//remove first element
		CDrawable* pDrawable = *(m_vObjList.begin());
		m_vObjList.erase(m_vObjList.begin());
		delete pDrawable;
	}

	//empty marked list
	m_vObjMarked.empty();

	return 0;
}

int CDrawList::EnableAll()
{
	// enable all objects
	for(unsigned int i=0; i<m_vObjList.size(); i++)
	{
		if(m_vObjList[i])
		{
			m_vObjList[i]->EnableBody();
		}
	}
	return 0;
}

int CDrawList::RemoveBalls()
{
	for(unsigned int i=0; i<m_vObjList.size(); i++)
	{
		if(m_vObjList[i])
		{
			if(m_vObjList[i]->GetType() == TYPE_BALL)
			{
				m_vObjList[i]->Remove();
			}
		}
	}
	return 0;
}

int CDrawList::Update()
{
	int iProblem = 0;
	// update state of all objects
	for(unsigned int i=0; i<m_vObjList.size(); i++)
	{
		if(m_vObjList[i])
		{
			if(m_vObjList[i]->Update() == -1)
			{
				iProblem = -1;
			}
			if(m_vObjList[i]->IsMarked())
			{
				MarkObject(m_vObjList[i]);
			}
		}
	}

	//delete unnecessary objects from list
	Clean();
	return iProblem;	//return -1 if any of the objects returned -1
}

int CDrawList::Draw()
{
	// draw all objects
	for(unsigned int i=0; i<m_vObjList.size(); i++)
	{
		if(m_vObjList[i])
		{
			m_vObjList[i]->Draw();
		}
	}
	return 0;
}

//get size of m_vObjList vector
int CDrawList::Size()
{
	return (int)m_vObjList.size();
}